<?php
namespace User\Controller;
use Think\Controller;
use Think\Model;

class UserController extends Controller {
	public function index() {
		$this -> display('user');
	}
	//修改密码
	public function pw_save(){
		

		if($_POST){
			$users = D('users');

			$arr = $users -> where("user_id = '{$_POST['user_id']}'") ->find();
			$password = $arr['password'];
			$old_pwd = md5($_POST['old_pwd']);
			$new_pwd = md5($_POST['new_pwd']);
			if($password != $old_pwd){
				echo 2; die();
			}
			$id = $users -> where("user_id = '{$_POST['user_id']}'") ->save(array('password' => $new_pwd)) ;
			if($id){
				echo 1;
			}

		}
	}
	// 挑战到下单页面
	public function place_order($user_name) {
		$obj = D('users');

		// select 返回 二维数组 , find 返回一位数组
		$result = $obj -> field('user_id') -> where("user_name= '$user_name'") -> find();

		$user_id = $result['user_id'];

		header("location: http://jj.we777.cc/index.php/user/user/index.html#/new_order/$user_id");
	}

	public function loginout() {
		cookie('name', null);
		return $this -> success('退出成功', '/');
	}

	public function da() {
		$obj = D('order');
		$z = $obj -> where("order_id>'100'") -> delete();

	}

	public function getMessage() {
		$name = I('name');
		$message = D('users') -> where(array('user_name' => $name)) -> select();
		echo json_encode($message);
	}

	public function updateMessage() {

		$data = I('post.');

		$update = D('users') -> where(array('user_id' => $data['user_id'])) -> save(array('sex' => $data['user_sex'], 'mobile' => $data['user_phone'], 'birthday' => $data['user_birthday'], ));

		if ($update) {
			echo 1;
		} else {
			echo 2;
		}
	}

	// 斌: 订单第一步骤 客户选择加工类型
	public function jglx() {
		if ($_POST) {
			$ptype = $_POST['jglx'];

			switch ($ptype) {
				case 1 :
					$ptype = '开料备材';
					break;
				case 2 :
					$ptype = '数控机加';
					break;

				case 3 :
					$ptype = '普通机加';
					break;

				case 4 :
					$ptype = '返金焊接';
					break;

				default :
					# code...
					break;
			}
			session('user_id', $_POST['user_id']);
			session('ptype', $ptype);
			$ss = session('ptype');
			print_r($ss);
		}
	}

	// 斌: 订单第二部 客户发送图片上来
	public function upload_file() {

		if ($_POST) {

			$cfg = array('rootPath' => './Public/upload/jj_upload/', 'autoSub' => false, );

			$upload = new \Think\Upload($cfg);
			// 多张图片上传
			$result = $upload -> upload($_FILES);

			// 实现获取多文件上传的路径 , 并且用 "," 隔开	, 用到里面没有定义的值 ,必须传值进来
			function get_uploadPath($result, $upload) {

				$result_str = '';

				$bigpathname = $upload -> rootPath;
				//图片的路径名

				foreach ($result as $key => $value) {
					$result_str .= $bigpathname . $value['savename'] . ",";
				}

				$result_str = substr($result_str, 0, -1);
				return $result_str;

			}

			$upload_path = get_uploadPath($result, $upload);

			date_default_timezone_set('PRC');
			$j = time();
			$sn = date('Ymdhis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
			//随机数

			// 订单号第三步要用到
			session('now_time', $j);
			session('order_number', $sn);
			session('upload_path', $upload_path);

			if ($upload_path) {
				echo '上传成功';
			}
		}
	}

	// 第三步 产品信息
	public function product_info() {
		if ($_POST) {
			$ptype = session('ptype');
			$userid = session('user_id');
			$j = session('now_time');
			$sn = session('order_number');
			$upload_path = session('upload_path');
			$product_name = $_POST['product_name'];
			$product_count = $_POST['product_count'];
			$product_date = $_POST['product_date'];

			$arr = array('order_sn' => $sn, 'ptype' => $ptype, 'attachment' => $upload_path, 'add_time' => $j, 'user_id' => $userid, 'allot_status' => 0, // 是否分配工序 , 刚刚上交订单 默认 0 没有分配
			'product_name' => $product_name, 'product_count' => $product_count, 'product_date' => $product_date);

			$obj = D('order');
			$info = $obj -> create();
			$z = $obj -> add($arr);
		}
	}

	// 通过user_id 获取 客户订单下单总数
	public function get_order_count() {

		$user_id = $_POST['user_id'];

		$sql = "select count(*) from ty_order where user_id='$user_id'";
		$result = D('order') -> query($sql);
		echo json_encode($result);
	}

	//用户页面 已下订单的显示
	public function get_order_infos() {

		$user_id = $_POST['user_id'];
		$results = D('order');
		$small = D('small_order');

		$sql = "select shipping_status ,pay_status,allot_status,total_amount,order_id,order_sn,order_status,shipping_status,pay_s_jj,add_time,already_offer from ty_order where user_id='$user_id' ";
		$result = $results -> query($sql);

		foreach ($result as $k => $v) {
			$order_sn = $v['order_sn'];
			$sql = "select * from ty_small_order where order_sn = '{$order_sn}'";
			$smallarr = $small -> query($sql);
			//	print_r(count($smallarr));
			$i = 0;
			foreach ($smallarr as $k => $v3) {//already_offer
				if ($v3['allot_status'] == 2 && $v3['audit'] == 1) {
					$i++;
				}
			}

			if ($i == count($smallarr) && $i != 0 && $v['already_offer'] != 2 && $v['already_offer'] != 4) {

				$arr = array('already_offer' => 1);
				$results -> where("order_sn = '{$order_sn}'") -> save($arr);
			}
			if ($v['already_offer'] != 4) {
				foreach ($smallarr as $k => $v2) {//already_offer
					if ($v2['jiaqian'] != 0) {
						$arr = array('already_offer' => 2);
						$results -> where("order_sn = '{$order_sn}'") -> save($arr);
					}
				}
			}
			$qian = $results -> where("order_sn = '{$order_sn}'") -> field('total_amount') -> select();

			if ($qian[0]['total_amount'] != 0 && $v['already_offer'] != 4) {

				$arr = array('already_offer' => 3);

				$results -> where("order_sn = '{$order_sn}'") -> save($arr);
			}
		}
		$results = D('order');
		// 总条数
		$count = $results -> where("user_id='{$user_id}'") -> count();
		if($count == 0 ){

		}else{
			// 首页显示 显示两条
		$P = 5;
		// 一页几多条信息
		$i = I('post.currentPage', '1');
		//页码数
		$j = ($i - 1) * $P;
		// 排序根据id降序
		$sql = "select already_offer,order_status,allot_status,total_amount,order_id,order_sn,order_status,shipping_status,pay_status,pay_s_jj,add_time from ty_order where user_id='$user_id' order by order_id DESC limit {$j},{$P}";
		$result = $results -> query($sql);

		$result[0]['count'] = $count;
		$result[0]['p'] = $P;
		//print_r($result);
		echo json_encode($result);
		}
		

	}

	//返回我的图号
	public function get_small_info() {
		$user_id = $_POST['user_id'];
		$sn = $_POST['order_sn'];
		$small = D('small_order');
		//图号显
		$sql = "select jiaqian, order_sn,chart_number,name,dates,type,remark,time_delivery,product_quantity  from ty_small_order , ty_users where ty_small_order.user_id = ty_users.user_id and order_sn = '{$sn}'";
		$orderList = $small -> query($sql);
		// 其他费用

		$order_other = D('order_other');

		$add = $order_other -> where(" order_sn = '{$sn}'") -> find();
		$orderList['order_other'] = $add;
		echo json_encode($orderList);
	}



	//客户订单——工序已经报价的显示。
	public function order_details_info() {
		//查询客户信息
		//第一步先查订单工序
		$user = D('admin');
		$gx = D('gx');
		$gxarr = $gx -> query("select * from ty_gx,ty_fpgx,ty_baojia where ty_fpgx.id =ty_gx.fpgxid AND ty_gx.id = ty_baojia.gxid AND ty_fpgx.dingdan = '{$_POST[order_sn]}' AND ty_fpgx.tuhao = '{$_POST[chart_number]}' 
		ORDER BY ty_gx.id ");
		// 将gxid一样的报价合并
		$ss;
		$gx_arr;
		$i = 0;
		$j = 2;
		$gxarr2 = $gxarr;

		foreach ($gxarr as $k => $v) {
			if ($k == 0) {
				$gx_arr[$i] = $v;

			} else if ($gx_arr[$i]['gxid'] == $v['gxid']) {

				$gx_arr[$i]['jiaqian' . $j] = $v['jiaqian'];
				$gx_arr[$i]['qiyename' . $j] = $v['qiyename'];
				$j = $j + 1;
			} else {
				$j = 2;
				$i = $i + 1;
				$gx_arr[$i] = $v;
			}
		}
		//print_r($gx_arr);
		// 第二步 将 微企的信息合拼
		$gx_ionf;
		foreach ($gx_arr as $k => $v) {
			//判断企业个数
			$length = count($v);
			$qiyelength = ($length - 8) / 2;
			//只有一家微企报价的情况
			if ($qiyelength == 1) {

				$qiye = $v['qiyename'];
				$sql = "select company_name,company_introduce,company_manager,company_photo from ty_admin where user_name= '{$qiye}'";
				$user_arr = $user -> query($sql);
				//print_r($user_arr);
				$gx_ionf[$k] = $v;
				$gx_ionf[$k]['company_name'] = $user_arr[0]['company_name'];
				$gx_ionf[$k]['company_introduce'] = $user_arr[0]['company_introduce'];
				$gx_ionf[$k]['company_manager'] = $user_arr[0]['company_manager'];
				$gx_ionf[$k]['company_photo'] = substr($user_arr[0]['company_photo'], 1);

			} else {
				//			// 多家微企的情况
				for ($i = 0; $i < $qiyelength; $i++) {
					if ($i == 0) {//第一家
						$qiye = $v['qiyename'];
						$sql = "select company_name,company_introduce,company_manager,company_photo from ty_admin where user_name= '{$qiye}'";
						$user_arr = $user -> query($sql);
						$gx_ionf[$k] = $v;
						$gx_ionf[$k]['company_name'] = $user_arr[0]['company_name'];
						$gx_ionf[$k]['company_introduce'] = $user_arr[0]['company_introduce'];
						$gx_ionf[$k]['company_manager'] = $user_arr[0]['company_manager'];
						$gx_ionf[$k]['company_photo'] = substr($user_arr[0]['company_photo'], 1);
					} else {//其他家
						$j = $i + 1;
						$qiye = $v['qiyename' . $j];
						$sql = "select company_name,company_introduce,company_manager,company_photo from ty_admin where user_name= '{$qiye}'";
						$user_arr2 = $user -> query($sql);
						//						print_r("2");

						$gx_ionf[$k]['company_name' . $j] = $user_arr2[0]['company_name'];
						$gx_ionf[$k]['company_introduce' . $j] = $user_arr2[0]['company_introduce'];
						$gx_ionf[$k]['company_manager' . $j] = $user_arr2[0]['company_manager'];
						$gx_ionf[$k]['company_photo' . $j] = substr($user_arr2[0]['company_photo'], 1);
						//
					}
				}

			}

		}
		//echo json_encode($gx_ionf);
		//print_r($gx_ionf);
		// 第三步 讲数据转成三维数组

		$gx_ionf_san;
		foreach ($gx_ionf as $k => $v) {
			//判断企业个数
			$length = count($v);
			$qiyelength = ($length - 8) / 6;

			if ($qiyelength == 1) {//只有一家企业报价
				$gx_ionf_san[$v['name']][0]['name'] = $v['name'];
				$gx_ionf_san[$v['name']][0]['jiaqian'] = $v['jiaqian'];
				$gx_ionf_san[$v['name']][0]['company_name'] = $v['company_name'];
				$gx_ionf_san[$v['name']][0]['company_introduce'] = $v['company_introduce'];
				$gx_ionf_san[$v['name']][0]['company_manager'] = $v['company_manager'];
				$gx_ionf_san[$v['name']][0]['company_photo'] = $v['company_photo'];
			} else {
				//			// 多家微企的情况
				for ($i = 0; $i < $qiyelength; $i++) {
					if ($i == 0) {//第一家
						$gx_ionf_san[$v['name']][$i]['name'] = $v['name'];
						$gx_ionf_san[$v['name']][$i]['jiaqian'] = $v['jiaqian'];
						$gx_ionf_san[$v['name']][$i]['company_name'] = $v['company_name'];
						$gx_ionf_san[$v['name']][$i]['company_introduce'] = $v['company_introduce'];
						$gx_ionf_san[$v['name']][$i]['company_manager'] = $v['company_manager'];
						$gx_ionf_san[$v['name']][$i]['company_photo'] = $v['company_photo'];
					} else {
						$j = $i + 1;
						$gx_ionf_san[$v['name']][$i]['name'] = $v['name'];
						$gx_ionf_san[$v['name']][$i]['jiaqian'] = $v['jiaqian' . $j];
						$gx_ionf_san[$v['name']][$i]['company_name'] = $v['company_name' . $j];
						$gx_ionf_san[$v['name']][$i]['company_introduce'] = $v['company_introduce' . $j];
						$gx_ionf_san[$v['name']][$i]['company_manager'] = $v['company_manager' . $j];
						$gx_ionf_san[$v['name']][$i]['company_photo'] = $v['company_photo' . $j];
					}

				}
			}

		}
		//冒泡排序处理三维数组
		foreach ($gx_ionf_san as $k => $v) {

			$len = count($v);
			for ($k = 0; $k <= $len; $k++) {
				for ($j = $len - 1; $j > $k; $j--) {
					if ($v[$j]['jiaqian'] < $v[$j - 1]['jiaqian']) {
						$temp = $v[$j];
						$v[$j] = $v[$j - 1];
						$v[$j - 1] = $temp;
					}
				}
			}

			$s[$v[0]['name']] = $v;

		}

		$gx_ionf_san = $s;
		echo json_encode($gx_ionf_san);

	}

	//新的客户订单的生成。
	public function orderDetails() {
		date_default_timezone_set('PRC');
		$d = date('y-m-d h', time());
		$j = time();
		$sn = date('Ymdhis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);

		$smallarr = $_POST;
		$order = D('order');
		$small = D('small_order');
		
		//判断图号是否唯一
		// foreach ($smallarr as $v) {
		// 	$id = $small -> where("chart_number ='{$v['photo_number']}' and user_id = '{$v['user_id']}' ") -> select();
		// 	if ($id) {
		// 		echo 2; 
		// 		exit();
		// 	}
		// }

		//生成订单
		$arr = array('order_sn' => $sn, 'add_time' => $j, 'user_id' => $smallarr[1]['user_id'], 'jj_provinces' => $smallarr[1]['addProvince'], 'consignee' => $smallarr[1]['addName'], 'mobile' => $smallarr[1]['addNumber'], 'address' => $smallarr[1]['address'], 'jj_area' => $smallarr[1]['addRegion'], 'jj_city' => $smallarr[1]['addCity'] );
		//20180104  生成订单其他费用比例表
		// dump($arr);
		// exit();
		$order_other = D('order_other');
		//订单其他费用比例表
		$other_expenses = D('other_expenses');
		//默认其他费用比例表
		$other_expensesarr = $other_expenses -> find();

		$other = array('order_sn' => $sn, 'technology' => $other_expensesarr['technology'], 'packaging' => $other_expensesarr['packaging'], 'management' => $other_expensesarr['management'], 'transport' => $other_expensesarr['transport'], 'cycle' => $other_expensesarr['cycle'], 'detection' => $other_expensesarr['detection'], 'tax' => $other_expensesarr['tax'], 'profits' => $other_expensesarr['profits'], 'judge' => 0);
		$order_other -> add($other);
		$orderid = $order -> add($arr);
		// 生成图号小订单

		if ($orderid) {
			foreach ($smallarr as $v) {
				if ($v['remarks'] == null) {
					$v['remarks'] = "无";
				}
				$sarr = array('order_sn' => $sn, 'chart_number' => $v['photo_number'], 'Product_quantity' => $v['product_number'], 'time_delivery' => $v['date'], 'name' => $v['product_name'], 'type' => $v['processing_type'], 'remark' => $v['remarks'], 'dates' => $d, 'user_id' => $smallarr[1]['user_id']);
				$small -> add($sarr);

			}
			echo $sn;
		}
		
	}

	//保存图号图片。
	public function orderDetaitp() {
		if ($_POST) {
			
			$cfg = array('rootPath' => './Public/upload/jj_upload/', 'autoSub' => false, );
			
			$upload = new \Think\Upload($cfg);
			// 图片上传
			$result = $upload -> upload($_FILES);
			$bigpathname = $upload -> rootPath;
			//图片地址
			$dizi = $bigpathname . $result['file']['savename'];
			$sql = "select image from ty_small_order where chart_number = '{$_POST['photo_number']}' and user_id = '{$_POST['user_id']}'  and order_sn = {$_POST['order_sn']}";
			$small = D('small_order');
			$m = $small -> query($sql);
			$arr = array('image' => $dizi);
			
			if ($m[0]['image'] == null) {
				//判断是否是第一张图片 进入表示是
				
				$small -> where("chart_number = '{$_POST['photo_number']}' and order_sn = '{$_POST['order_sn']}'") -> save($arr);

				// 根据条件更新记录
			} else {
				$arr = array('image' => $m[0]['image'] . "," . $dizi);
				$small -> where("chart_number = '{$_POST['photo_number']}' and order_sn ='{$_POST['order_sn']}'") -> save($arr);
				// 根据条件更新记录
			}

		}
	}

	// 图号的显示
	public function tuhaolist() {
		if ($_POST) {
			print_r($_POST);
		}
		$this -> display();
	}

	//chart_price 保存图号价钱表   
	public function chart_price_add() {

		//PHP 接受 JSON 函数
		$json = $GLOBALS['HTTP_RAW_POST_DATA'];
		$arr = json_decode($json, TRUE);
		$chart_price = D('chart_price');
		$baojia = D('baojia');
		$user = D('admin');
		$s = count($arr);
		$i = 0;
		foreach ($arr as $k => $v) {
			if ($k <= ($s - 4)) {
				$chart_pricearr[$k] = $v;
			} else if ($k > ($s - 4)) {
				$chartarr[$i] = $v;
				$i++;
			}
		}
		
		foreach ($chartarr as $k => $v) {
			if ($k == 0) {
				$chart_number = $v['chart_number'];
			} else if ($k == 1) {
				$order_sn = $v['order_sn'];
			} else if ($k == 2) {
				$total_price = $v['total_price'];
			}
		}
		$total_prices = 0;
		
		foreach ($chart_pricearr as $v) {

			
			$arr = $user -> where("company_name = '{$v['company_name']}'") ->find();
			$qiyename = $arr['user_name'];
			$jiaqian = $baojia ->query("select ty_baojia.jiaqian from ty_baojia,ty_gx,ty_fpgx where ty_fpgx.id = ty_gx.fpgxid and ty_gx.id = ty_baojia.gxid and ty_fpgx.tuhao = '{$chart_number}' and ty_baojia.qiyename = '{$qiyename}'  and ty_gx.name = '{$v['name']}' and ty_fpgx.dingdan = '{$order_sn}'");
			
			if(!empty($jiaqian)){
				$jiaqian = $jiaqian[0]['jiaqian'];
				//$total_prices = $jiaqian + $total_prices;
				$total_prices = $jiaqian + $total_prices;
			$arr = array('order_sn' => $order_sn, 'chart_number' => $chart_number, 'qiyename' => $v['company_name'], 'gx_name' => $v['name'], 'gx_jiaqian' => $jiaqian);
			$id = $chart_price -> add($arr);
			if (!$id) {
				exit();
			}
			}

			
		}
		
		$small = D('small_order');
		$add = array('jiaqian' => $total_prices);
		$id = $small -> where("order_sn = '{$order_sn}' and chart_number = '{$chart_number}'") -> save($add);

		if ($id) {
			echo 1;
		}
	}
//图号的更新。
	public function order_details_update() {
		$json = $GLOBALS['HTTP_RAW_POST_DATA'];
		$arr = json_decode($json, TRUE);
		$chart_price = D('chart_price');
		$baojia = D('baojia');
		$user = D('admin');
		$s = count($arr);
		$i = 0;

		foreach ($arr as $k => $v) {
			if ($k <= ($s - 4)) {
				$chart_pricearr[$k] = $v;
			} else if ($k > ($s - 4)) {
				$chartarr[$i] = $v;
				$i++;
			}
		}

		foreach ($chartarr as $k => $v) {
			if ($k == 0) {
				$chart_number = $v['chart_number'];
			} else if ($k == 1) {
				$order_sn = $v['order_sn'];
			} else if ($k == 2) {
				$total_price = $v['total_price'];
			}
		}
		$total_prices = 0;
		foreach ($chart_pricearr as $v) {

			$arr = $user -> where("company_name = '{$v['company_name']}'") ->find();
			$qiyename = $arr['user_name'];
			$jiaqian = $baojia ->query("select ty_baojia.jiaqian from ty_baojia,ty_gx,ty_fpgx where ty_fpgx.id = ty_gx.fpgxid and ty_gx.id = ty_baojia.gxid and ty_fpgx.tuhao = '{$chart_number}' and ty_baojia.qiyename = '{$qiyename}'  and ty_gx.name = '{$v['name']}' and ty_fpgx.dingdan = '{$order_sn}' ");
			
			if(!empty($jiaqian)){
				$jiaqian = $jiaqian[0]['jiaqian'];
				$total_prices = $jiaqian + $total_prices;
			$arr = array('qiyename' => $v['company_name'], 'gx_jiaqian' => $jiaqian);
			$id = $chart_price -> where("gx_name ='{$v['name']}' and order_sn = '{$order_sn}' and chart_number = '{$chart_number}'") -> save($arr);
			
}
		}
		
		$small = D('small_order');
		$add = array('jiaqian' => $total_prices);

		$id = $small -> where("order_sn = '{$order_sn}' and chart_number = '{$chart_number}'") -> save($add);
		echo 1;

	}
	// 签合同和保存订单总价钱   othercost  订单其他费用
	public function contract_details() {

		$json = $GLOBALS['HTTP_RAW_POST_DATA'];
		$arr = json_decode($json, TRUE);

		$chart_price = D('chart_price');
		$order = D('order');
		$small = D('small_order');
		$total_amounts = 0;
		foreach ($arr as $k => $v) {
			if ($k == 'total') {
				//$arr = array('total_amount' => $v);
				$total_amount = $v;
			} else if ($k == 'other_total') {
				$othercost = $v;
			} else if ($k == 'order_sn') {
				$sn = $v;
			} else if ($k == 'user_id') {
				$userid = $v;
			} else {
				$jqarr = $small ->where("order_sn = '{$v['order_sn']}' and chart_number = '{$v['chart_number']}' ") ->find();
				$total_amounts = $jqarr['jiaqian']*$jqarr['product_quantity'] +$total_amounts;
				$smallarr = array('judge_choose' => 1);
				$small -> where("order_sn = '{$v['order_sn']}' and chart_number = '{$v['chart_number']}' ") -> save($smallarr);
				$chart_price ->where("order_sn = '{$v['order_sn']}' ") ->save($smallarr);//更新图号价格表状态
			}

		}
		$order_other = D('order_other');
		$add = $order_other -> where(" order_sn = '{$sn}'") -> find();
		if($add['judge'] == 1 ){
			$othercosts = $add['technology'] + $add['packaging'] + $add['management']+ $add['transport']+ $add['cycle']+ $add['detection']+ $add['profits'] ;//其他费用

		}else{
			$fees = $add['technology'] + $add['packaging'] + $add['management']+ $add['transport']+ $add['cycle']+ $add['detection']+ $add['profits'] ;
			$othercosts = $total_amounts*$fees*0.01;//其他费用	
		}			
		$total_amount_a = $othercosts+$total_amounts;
		$tax = $total_amount_a*$add['tax']*0.01;//税费
		$total_amount = $tax + $total_amount_a;
		//总费用
		$arr = array('total_amount' => $total_amount, 'othercost' => $othercosts, 'tex' => $tax);
		$id = $order -> where("order_sn = '{$sn}' and user_id = '{$userid}'") -> save($arr);
		if ($id) {
			echo 1;

		}
	}

	//地址判断
	public function address_judge() {
		if ($_POST) {
			$sql = "select ty_users.address_judge from ty_users where ty_users.user_id = '{$_POST['user_id']}' ";
			$users = D('users');
			$users = $users -> query($sql);

			echo $users[0]['address_judge'];
		}

	}

	//保存地址
	public function add_judge() {

		if ($_POST) {
			$user_id = $_POST['user_id'];
			$users = D('users');
			$add = array('jj_province' => $_POST['province'], 'jj_city' => $_POST['city'], 'jj_region' => $_POST['region'], 'jj_receiver' => $_POST['receiver'], 'jj_post' => $_POST['post'], 'jj_detail_address' => $_POST['detail_address'], 'jj_user_phone' => $_POST['user_phone'], );
			$id = $users -> where("user_id = '{$_POST['user_id']}'") -> save($add);

			if ($id) {

				$sql = "UPDATE ty_users SET address_judge = 1 WHERE user_id = {$user_id}";
				$users -> query($sql);
				$id2 = $users -> field("address_judge") -> where("user_id = {$user_id}") -> find();
				if ($id2['address_judge'] == 1) {
					echo 1;
				}
			}
		}
	}

	//地址显示 	如果已经填写地址就返还地址信息没有就返回0
	public function find_judge() {
		if ($_POST) {
			$user_id = $_POST['user_id'];
			$user_name = $_POST['user_name'];
			$sql = "select address_judge,jj_province,jj_city,jj_region,jj_receiver,jj_post,jj_detail_address,jj_user_phone from ty_users where ty_users.user_id = '{$user_id}' and ty_users.user_name = '{$user_name}' ";
			$users = D('users');
			$users = $users -> query($sql);

			if ($users[0]['address_judge'] == 0) {
				echo $users[0]['address_judge'];
			} else {
				echo json_encode($users);
			}
		}
	}

	//合同显示
	public function find_hetong() {
		$contract = D('contract');
		$arr = $contract -> select();
		echo json_encode($arr);
	}

	//支付页面已选择订单图号的显示
	public function find_pay_order() {
		if ($_POST) {
			$order = D('order');
			$ordersmall = D('small_order');
			$small = $ordersmall -> where("order_sn = '{$_POST['order_sn']}' ") -> select();
			//		$orders = $order ->where("order_sn = '{$_POST['order_sn']}' ") -> find();

			$sql = "UPDATE ty_order SET already_offer = 4 WHERE order_sn = '{$_POST['order_sn']}'";
			$orderid = $order -> query($sql);
			$ordersql = " select othercost,total_amount,tex from ty_order where order_sn = '{$_POST['order_sn']}'";
			$orderlist = $order -> query($ordersql);
			$smallarr;

			$i = 0;
			foreach ($small as $k => $v) {
				if ($v['judge_choose'] == 1) {
					$smallarr[$i] = $v;
					$i++;
				}
			}
			//		$user_id = $_POST['user_id'];
			//
			//			$sql = "select address_judge,jj_province,jj_city,jj_region,jj_receiver,jj_post,jj_detail_address,jj_user_phone from ty_users where ty_users.user_id = '{$user_id}' ";
			//			$users = D('users');
			//			$users = $users -> query($sql);

			$smallarr['othercost'] = $orderlist[0]['othercost'];
			$smallarr['total_amount'] = $orderlist[0]['total_amount'];
			$smallarr['tax'] = $orderlist[0]['tex'];

			echo json_encode($smallarr);
		}
	}

	// 图号判断
	public function tuhao_panduan() {
		$order = D('order');
		$small = D('small_order');
		$user_id = $_POST['user_id'];
		$sql = "select ty_small_order.chart_number from ty_order, ty_small_order where ty_order.order_sn = ty_small_order.order_sn and ty_order.user_id = '{$user_id}'";
		$order = $order -> query($sql);

		foreach ($order as $v) {
			if ($v['chart_number'] == $_POST['photo_number']) {
				echo 1;
				exit();
			}
		}
	}
	
	//订单的修改
	// 删除订单内的所有图号 从新下图号
	public function order_save(){
		//delete
		if($_POST){
//		$fpgx = D('fpgx');
//		$gx = D('gx');
//		$fpgxIdList = $fpgx -> where("dingdan ='{$_POST['order_sn']}' ") ->select();
//		$j = count($fpgxIdList);
//		$i =0;
//		foreach($fpgxIdList as $k =>$v){
//			$id = $gx ->where("fpgxid = '{$v['id']}'") ->delete();
//			if($id){
//				$i++;
//			}
//		}
//	if($j==$i){
//		$fpgxid = $fpgx -> where("dingdan ={$_POST['order_sn']} ") ->delete();
//		if($fpgxid){
//			echo 1 ;
//		}
//		
//	}

	$small = D("small_order");
	$id = $small -> where("order_sn = '{$_POST['order_sn']}'") -> delete();	
	if($id){
		echo 1;
	}


	}
	}
	//生成图号
	public function order_save_add(){
		date_default_timezone_set('PRC');
		$d = date('y-m-d h', time());
		$small = D("small_order");
		$smallarr =$_POST;
		foreach ($smallarr as $v) {
			$sn = $v['order_sn'];
				if ($v['remarks'] == null) {
					$v['remarks'] = "无";
				}
				$sarr = array('order_sn' => $sn, 'chart_number' => $v['photo_number'], 'Product_quantity' => $v['product_number'], 'time_delivery' => $v['date'], 'name' => $v['product_name'], 'type' => $v['processing_type'], 'remark' => $v['remarks'], 'dates' => $d, 'user_id' => $v['user_id']);
				$small -> add($sarr);

			}
		echo 1 ;
	}
	//添加图号里面的图片
	public function order_save_small(){
		if ($_POST) {
			$cfg = array('rootPath' => './Public/upload/jj_upload/', 'autoSub' => false, );
			var_dump($_POST['order_sn']);
			$upload = new \Think\Upload($cfg);
			// 图片上传
			$result = $upload -> upload($_FILES);
			$bigpathname = $upload -> rootPath;
			//图片地址
			$dizi = $bigpathname . $result['file']['savename'];
			$sql = "select image from ty_small_order where chart_number = '{$_POST['photo_number']}' and user_id = '{$_POST['user_id']}' and order_sn = '{$_POST['order_sn']}'";
			$small = D('small_order');
			$m = $small -> query($sql);
			$arr = array('image' => $dizi);

			if ($m[0]['image'] == null) {
				//判断是否是第一张图片 进入表示是
				$small -> where("chart_number = '{$_POST['photo_number']}' and order_sn = '{$_POST['order_sn']}'") -> save($arr);
				// 根据条件更新记录
			} else {
				$arr = array('image' => $m[0]['image'] . "," . $dizi);
				$small -> where("chart_number = '{$_POST['photo_number']}' and order_sn = '{$_POST['order_sn']}'") -> save($arr);
				// 根据条件更新记录
			}
	}
	}
	//订单删除
	public function order_delete(){
		if($_POST){
		$order = D("order");
		$small = D("small_order");
		$order_other = D("order_other");
		$id1 =$small ->where("order_sn = '{$_POST['order_sn']}'") ->delete();
		$id2 = $order_other ->where("order_sn = '{$_POST['order_sn']}'") ->delete();
		$id3  = $order ->where("order_sn = '{$_POST['order_sn']}'") ->delete();
		if($id1&$id2&$id3){
			echo 1; 
		}
	
	}
	}	
	//查看订单图号未报价信息
	public function order_small_find(){
		if($_POST){
		$small = D("small_order");
		$arr = $small ->where("order_sn = '{$_POST['order_sn']}'") ->select();
		echo json_encode($arr);
	}
	
	}
	//进入hetong页面
	public function processing_state(){
		if($_POST){
			// $processing_state ->where("order_sn = '{$_POST['order_sn']}'") -> find();
			// if(){

			// }
		$small_order  =D("small_order");
		$chart_price = D('chart_price');
		$processing_state = D('processing_state');
				
				$sql2 = "select ty_chart_price.chart_number from ty_chart_price where ty_chart_price.order_sn = '{$_POST['order_sn']}' and ty_chart_price.judge_choose = 1 GROUP BY  ty_chart_price.chart_number";
				$tuhao = $chart_price -> query($sql2);

foreach ($tuhao as $key => $th) {
	$sql = "select ty_chart_price.gx_name,ty_chart_price.qiyename,ty_chart_price.order_sn,ty_chart_price.chart_number from ty_chart_price where ty_chart_price.order_sn = '{$_POST['order_sn']}' and ty_chart_price.judge_choose = 1  and ty_chart_price.chart_number = '{$th['chart_number']}'";
	$chartlist  =	$chart_price -> query($sql);
	$small = $small_order ->where("chart_number = '{$th['chart_number']}'") -> find();
		foreach ($chartlist as $key => $value) {
					
					 $arr = array('order_sn' => $value['order_sn'], 'chart_number' => $value['chart_number'], 'qiyename' => $value['qiyename'], 'gx_name' => $value['gx_name'], 'state' => 0, 'remark' => $small['remark'], 'time_delivery' => $small['time_delivery']);
					$processing_state -> add($arr);
					
				}
}
				
	}			
				
	}	



	//首款支付订单详情
	public function order_firstarr(){
		if($_POST){
			$order = D('order');
			$order_firstarr = $order ->where("order_sn = {$_POST['order_sn']}") -> field('first_je,first_time,first_jy_sn,first_sn') ->find();
			$i = substr($order_firstarr['first_jy_sn'], -1);
			if($i == 1 ){
				$order_firstarr['pay_a'] = 1;
			}elseif($i == 2){
				$order_firstarr['pay_a'] = 2;
			}
			$order_firstarr['first_jy_sn']= substr($order_firstarr['first_jy_sn'],0,strlen($order_firstarr['first_jy_sn'])-1);
			echo json_encode($order_firstarr);
		}
}
  
	
	//尾款支付订单详情
	public function tail_paragrapharr(){
		if($_POST){
			$order = D('order');
			$order_firstarr = $order ->where("order_sn = {$_POST['order_sn']}") -> field('first_je,first_time,first_jy_sn,first_sn') ->find();
			$i = substr($order_firstarr['first_jy_sn'], -1);
			if($i == 1 ){
				$order_firstarr['pay_a'] = 1;
			}elseif($i == 2){
				$order_firstarr['pay_a'] = 2;
			}
			$order_firstarr['first_jy_sn']= substr($order_firstarr['first_jy_sn'],0,strlen($order_firstarr['first_jy_sn'])-1);
			


			$tail_paragrapharr = $order ->where("order_sn = {$_POST['order_sn']}") -> field('tail_je,tail_time,tail_jy_sn,tail_sn') ->find();
			$i = substr($tail_paragrapharr['tail_jy_sn'], -1);
			if($i == 1 ){
				$tail_paragrapharr['pay_a'] = 1;
			}elseif($i == 2){
				$tail_paragrapharr['pay_a'] = 2;
			}
			$tail_paragrapharr['tail_jy_sn']= substr($tail_paragrapharr['tail_jy_sn'],0,strlen($tail_paragrapharr['tail_jy_sn'])-1);
			$arr[0] =$order_firstarr;
			$arr[1] =$tail_paragrapharr; 
			
			echo json_encode($arr);
		}
}

	//尾款待交金额
   public function order_tail(){
   	if($_POST){
   		$order = D('order');
			$order_tail = $order ->where("order_sn = {$_POST['order_sn']}") -> field('total_amount,has_pay_amount') ->find();
			echo json_encode($order_tail);
   	}
   }
	//全额交易信息
	public function order_in_fullarr(){
				if($_POST){
					$order = D('order');

   		$order_in_fullarr = $order ->where("order_sn = {$_POST['order_sn']}") -> field('in_full_je,in_full_time,in_full_jy_sn,in_full_sn') ->find();
				}

			$i = substr($order_in_fullarr['in_full_jy_sn'], -1);
			if($i == 1 ){
				$order_in_fullarr['pay_a'] = 1;
			}elseif($i == 2){
				$order_in_fullarr['pay_a'] = 2;
			}
				$order_in_fullarr['first_jy_sn']= substr($order_in_fullarr['first_jy_sn'],0,strlen($order_in_fullarr['first_jy_sn'])-1);
				echo json_encode($order_in_fullarr);
	}
  //加工信息
   public function order_in_fulltuhao(){
   	if($_POST){
 
   		$processing_state = D('processing_state');
   		
   		$a = $processing_state -> where("order_sn = '{$_POST['order_sn']}' ") ->  field("chart_number") -> select();
   			//将一样的图号拼一起
   		foreach ($a as $k => $v) {
   			$a[$k] = $v['chart_number'];
   		}
   		
   		$arr =array_unique($a);
   		
   		foreach ($arr as $key => $value) {
   			$processing = $processing_state -> where("order_sn = {$_POST['order_sn']} and chart_number = '{$value}' ") ->   select();
   			$tuhaoarr[$processing['0']['chart_number']] = $processing;
   			

   			
   		}
   			//$tuhaoarr['in_full'] = $order_in_fullarr;// 图号和支付数组拼接
   			//print_r($tuhaoarr);
			echo json_encode($tuhaoarr);
   	}
   }
//下订单是显示客户的物流地址
   public function address_info(){
   	if($_POST){}
   			header("Content-Type: text/html;charset=utf-8"); 
   			$users = D('users');
   			$id = $_POST['user_id'];
   			$usersarr = $users ->where("user_id = '{$id}' ") ->find();
   			//$arr['address'] = $usersarr['jj_province'].$usersarr['jj_city'].$usersarr['jj_region'].$usersarr['jj_detail_address'];
   			
   			$arr['jj_province'] = $usersarr['jj_province'];
   			$arr['jj_city'] = $usersarr['jj_city'];
   			$arr['jj_region'] = $usersarr['jj_region'];
			$arr['jj_detail_address'] = $usersarr['jj_detail_address'];
   			$arr['jj_post'] = $usersarr['jj_post'];
   			$arr['jj_user_phone'] = $usersarr['jj_user_phone'];
   			$arr['jj_receiver'] = $usersarr['jj_receiver'];
   			
   				
   			echo json_encode($arr);
   		
   		
   }

   //收货地址
   public function delivery_info(){
   	header("Content-Type: text/html;charset=utf-8"); 
   	if($_POST){//create_time
   		$delivery_doc = D("delivery_doc");
   		$delivery_doc_list = $delivery_doc -> where("order_sn = '{$_POST['order_sn']}'") ->find();
   		$delivery_doc_list['create_time']   =date("Y-m-d H:i",$delivery_doc_list['create_time']);
   		echo json_encode($delivery_doc_list);
   	}
   }
   // 确认收货
   public function delivery_save(){
   		if($_POST){
   		$order = D("order");
   		$arr = array('shipping_status' => 3 );
   		
   		$id = $order -> where("order_sn = '{$_POST['order_sn']}'") -> save($arr);
   		  
 
   		if($id){
   			echo 1;
   		}
   		
   	}
   }
	public function post(){
		$json = $GLOBALS['HTTP_RAW_POST_DATA'];
	$arr = json_decode($json, TRUE);
	var_dump($json);
	}
	//服务内容
	public function serviceContentInfo(){

		$service = D('service_content');
		if($_GET){
			$arr = $service -> where("id = {$_GET['id']}") ->find();
			$this -> assign('arr', $arr);
			$this -> display();
		}
	}
	//	
	public function jj_enterpriseInfo(){
		$jj_enterprise = D('jj_enterprise');
		if($_GET){
		$arr = $jj_enterprise -> where("id = {$_GET['id']}") ->find();
		$this -> assign('arr', $arr);
		$this -> display();
		}
	}
	public function toJoinAdd(){
		$toJoin = D('tojoin');
		$arrs = $_POST;
			
			 $arr = array('name' => $arrs['user_name'], 'mobile' => $arrs['user_phone'], 'time' => time(), 'email' =>$arrs['user_email'],'address' =>$arrs['user_address'],'intro' =>$arrs['user_inof'] );
			$id = $toJoin ->add($arr);
			if($id){
					return $this -> success('提交成功', '/');
			}
		
	}
}
